package net.xxcl.zcgl.controller;

import net.xxcl.zcgl.pojo.Criteria;
import net.xxcl.zcgl.pojo.Role;
import net.xxcl.zcgl.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: 夏汛
 * Date: 12-1-15
 * Time: 下午8:30
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 获取角色列表
     * @param start
     * @param limit
     * @return
     */
    @RequestMapping(value = "/roles")
    public @ResponseBody Map<String,Object> getKemus(@RequestParam(value = "start",required = false) Integer start,
                                                     @RequestParam(value = "limit",required = false) Integer limit){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        int total = roleService.countByExample(criteria);
        if(start!=null && limit!=null){
            criteria.setMysqlOffset(start);
            criteria.setMysqlLength(limit);
        }
        List<Role> list = roleService.selectByExample(criteria);
        responseMap.put("total",total);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 新增或编辑
     * @param role
     * @return
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> saveOrUpdate(@ModelAttribute("role") Role role,@RequestParam("menuIds") String menuIds){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try{
            //新增角色
            if(role.getId()==null){
                roleService.insertRole(role,menuIds);
                responseMap.put("method", "Create");
                responseMap.put("success", "true");
                responseMap.put("info", "新增成功！");
            }
            //编辑角色
            else {
                roleService.updateRole(role.getId(),menuIds);
                responseMap.put("success", "true");
                responseMap.put("info", "编辑成功！");
            }
        } catch (Exception e){
            e.printStackTrace();
            responseMap.put("info", e.getClass()+":"+e.getMessage());
        }
        return responseMap;
    }

    /**
     * 编辑角色时，勾选角色有的菜单
     * @param id
     * @return
     */
    @RequestMapping(value="/getMenuIdsByRole",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> getMenuIdsByRole(@RequestParam("id") Integer id){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try{
            responseMap.put("menuids",roleService.getMenuIdsByRole(id));
            responseMap.put("success","true");
        } catch (Exception e){
            e.printStackTrace();
            responseMap.put("info", "服务器出错，未能正确勾选已有权限！");
        }
        return responseMap;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping(value="/remove",method=RequestMethod.POST)
    public @ResponseBody Map<String,Object> remove(@RequestParam("ids") String ids){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try {
            roleService.deleteRole(ids);
            responseMap.put("success", "true");
            responseMap.put("info", "删除成功！");
            return responseMap;
        }catch(Exception e) {
            e.printStackTrace();
            responseMap.put("info", e.getMessage());
            return responseMap;
        }
    }
}
